package com.mactivity.database;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.mactivity.helper.Constants;
/**
 * Author: Sanjay Vasudeva Iyer
 * Class NotificationActivityFunctions - Handles all the database calls made as part of bringing up Notification
 * */
public class NotificationActivityFunctions {

	/**
	 * This method will get the Notifications from the Notifications table
	 * These are shown in Notifications board.
	 * */
	public String getNotifications(Integer I) {
		String response=null;
		try{
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_USERID,I.toString()));

			Log.d(Constants.LOG_DATABASE_TAG, "Connecting to database");
			//getting from the Notification table, where NoteTo column is the UserID
			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/notifications_script.php",postParameters);
 			Log.d(Constants.LOG_DATABASE_TAG, "Got data from database");
		}
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}

		return response;
	}
	
	/**
	 * This cleans up the already read Notifications from table Notifications
	 */
	public void deleteNotification(Integer I) {
		try {
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.COLUMN_NOTIFICATION_NOTEID, I.toString()));
			//Deleting from the Notification table passing the note ID
			CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/delete_notification_script.php",postParameters);
		} 
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}
	}

	/**
	 * This method will get the group plans to which the user has made himself from the Group Plan table
	 * These are shown as MG(My Group Plan) in the dashbboard.
	 * */
	public void acceptrejectGroupPlan(Integer N,Integer F,Integer T,Integer A) {
		try{
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.COLUMN_NOTIFICATION_NOTEID,N.toString()));
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FROMID,F.toString()));
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_TOID,T.toString()));
			postParameters.add(new BasicNameValuePair(Constants.COLUMN_NOTIFICATION_TYPESTATUS,A.toString()));

			Log.d(Constants.LOG_DATABASE_TAG, "Connecting to database");
			//getting from the GroupPlan table, where GroupPlanOwner column is the UserID
			CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/accept_groupplan_script.php",postParameters);
 			Log.d(Constants.LOG_DATABASE_TAG, "Got data from database");
		}
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}
	}
	/**
	 * This method will get the Group Plan details of the Group Plan to which he is invited
	 * */
	public String getGroupPlanRequestDetails(Integer I) {
		String response=null;
		try{
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.COLUMN_NOTIFICATION_GROUPPLANID,I.toString()));

			Log.d(Constants.LOG_DATABASE_TAG, "Connecting to database");
			//getting from the Notification table, where NoteTo column is the UserID
			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/fetch_group_plan_script.php",postParameters);
 			Log.d(Constants.LOG_DATABASE_TAG, "Got data from database");
		}
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}

		return response;
	}
	/**
	 * This method will accept a friend request
	 * */
	public void acceptRejectFriendRequest(Integer N,Integer F,Integer T,Integer A) {
		try{
			//Post parameters for Notification table
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.COLUMN_NOTIFICATION_NOTEID,N.toString()));
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_USERID,F.toString()));
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_TOID,T.toString()));
			postParameters.add(new BasicNameValuePair(Constants.COLUMN_NOTIFICATION_TYPESTATUS,A.toString()));

			Log.d(Constants.LOG_DATABASE_TAG, "Connecting to database");
			//Change the Notification table to make the change to TypeStatus to 2 and swap NoteTo and NoteFrom
			CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/accept_friend_script.php",postParameters);
			Log.d(Constants.LOG_DATABASE_TAG, "Got data from database");
		}
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}
	}
	/*
	 * Adds 2 user ids as Friends in Friends table
	 * */
	public void addAsFriends(Integer F,Integer T) {
		try{
			//Post parameters for Friend Table
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_MYID,F.toString()));
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FRIENDID,T.toString()));

			Log.d(Constants.LOG_DATABASE_TAG, "Connecting to database");
			//Add the 2 user ids as Friends in the Friends table
			CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/add_friend_script.php",postParameters);
 			Log.d(Constants.LOG_DATABASE_TAG, "Got data from database");
		}
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}
	}
	/*
	 * Get the user details when provided with the userid
	 * */
	public String fetchUserDetails(Integer I) {
		String response=null;
		try{
			//Post parameters for Friend Table
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_USERID,I.toString()));
			
			Log.d(Constants.LOG_DATABASE_TAG, "Connecting to database");
			//Add the 2 user ids as Friends in the Friends table
			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/fetch_user_details_script.php",postParameters);
 			Log.d(Constants.LOG_DATABASE_TAG, "Got data from database");
		}
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}
		return response;
	}
	/*
	 * Plans clash, this method can delete the old individual plan to accept the new group plan
	 * */
	public void deleteOldIP(String date, String time, Integer T) {
		// TODO Auto-generated method stub
		ArrayList<NameValuePair> postParametersConflict = new ArrayList<NameValuePair>();
		postParametersConflict.add(new BasicNameValuePair(Constants.COLUMN_GROUPPLAN_DATE,date));
		postParametersConflict.add(new BasicNameValuePair(Constants.COLUMN_GROUPPLAN_TIME,time));
		postParametersConflict.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_USERID,T.toString()));
		try {
			//delete tuple from individual plan table
			CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/delete_old_individualplan_acceptGP.php",postParametersConflict);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.d(Constants.LOG_DATABASE_TAG, "Exception in deleteOldIP");
		}
	
	}
	/*
	 * When accepting a new group request, reject the old one.
	 * */
	public void rejectOldAcceptedGP(String noteId) {
		// TODO Auto-generated method stub
		ArrayList<NameValuePair> postParametersConflict = new ArrayList<NameValuePair>();
		postParametersConflict.add(new BasicNameValuePair(Constants.COLUMN_NOTIFICATION_NOTEID,noteId));
		try {
			//Rejecting the old plan while accepting the new one.
			CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/reject_oldaccepted_groupplan.php",postParametersConflict);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.d(Constants.LOG_DATABASE_TAG, "Exception in rejectOldAcceptedGP");
		}
	}

	/*
	 * Method gets Notification table NoteId column for a group plan which has already been accepted.
	 * */
	public String getNoteIDAcceptedGroupPlan(String date, String time, Integer T) {
		// TODO Auto-generated method stub
		ArrayList<NameValuePair> postParametersConflict = new ArrayList<NameValuePair>();
		postParametersConflict.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_CLASH_DATE,date));
		postParametersConflict.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_CLASH_TIME,time));
		postParametersConflict.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_CLASH_USERID,T.toString()));
		String response=null;
		try {
			//returns NoteId and the GroupPlanId
			 response=CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Notifications/accepted_group_conflict.php",postParametersConflict);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.d(Constants.LOG_DATABASE_TAG, "Exception in getNoteIDAcceptedGroupPlan");
		}

		return response;
	}
}
